package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspWriter;

import Model.Appointment;

/**
 * Servlet implementation class ApptResponsibilities
 */

public class ApptResponsibilities extends HttpServlet {
	private static final long serialVersionUID = 1L;
	JspWriter out;  
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ApptResponsibilities() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    public ApptResponsibilities(JspWriter outputStream) {
		// TODO Auto-generated constructor stub
		out = outputStream;
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		//get parameter from url
		retrieveResp(request, response);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		retrieveResp(request, response);
	}

	private void retrieveResp(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		
		Appointment apptTable = new Appointment();
		apptTable = Database.AppointmentDB.getAppointmentbyID(id);
		
		String appointment = apptTable.getName();
		String responsibilities = apptTable.getResponsibilities();
		responsibilities = responsibilities.replaceAll("(\r\n|\n)", "<br />");;
		
		System.out.println("responsibilities : " + responsibilities);
		PrintWriter out = null;
		try {
			out = response.getWriter();
		
			out.print("<table class=\"tablesorter tableproccess\" border=\"0\" cellspacing=\"0\" style=\"width:80%\">");
			out.print("<tr><td class=\"blueprocessheader\">");
			out.print("<h2 class=\"blueprocessh1\">" + appointment + "</h2>");
			out.print("</td></tr>");
			out.print("<tr><td class=\"oddprocessth\">");
			out.print("Responsibilities: ");
			out.print("</td></tr>");
			out.print("<tr><td class=\"evenprocessth\">");
			out.print(responsibilities);
			out.print("</td></tr></table>");	
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
